//
//  LibraryViewController.m
//  我的图书馆
//
//  Created by 孙  路 on 15-1-4.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LibraryViewController.h"

#import "HoldingView.h"

#import "ListViewController.h"

#import "HoldingViewController.h"
//收藏文章cell
#import "LibraryTableViewCell.h"
//编辑文章
#import "EditArticleViewController.h"
//进入搜索界面
#import "SearchViewController.h"
//读取数据库
#import "HistoryBace.h"
#import "HistoryModel.h"
//主色调
#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]
@interface LibraryViewController ()
//我的下载
@property (nonatomic, retain) UIAlertView *alert;
@property (nonatomic, retain) UIAlertView *alert1;
@end

@implementation LibraryViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"myDownLoad" object:nil];
    [_tableArray release];
    [_buttonArray release];
    [_tableV release];
    [_alert release];
    [_alert1 release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.buttonArray = [NSMutableArray array];

        self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStyleGrouped];
        [_tableV registerClass:[LibraryTableViewCell class] forCellReuseIdentifier:@"libraryCell"];
        [_tableV setBackgroundColor:[UIColor whiteColor]];
        _tableV.delegate = self;
        _tableV.dataSource = self;

        [self createLabelCenter];
        [self createView];

        [self.view addSubview:_tableV];
        [_tableV release];

    }
    return self;
}
- (void)viewDidLoad {
    //[self readData];
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = kBlueColor;
   // self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.hidden = NO;

   


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createAlert) name:@"myDownLoad" object:nil];

//    [self createAddButton];
}


#pragma mark -
#pragma mark 读取数据库
- (void)readData
{
    [_tableArray removeAllObjects];
    _tableArray = (NSMutableArray *)[HistoryBace selectAllHistory];
    NSLog(@"%@", _tableArray);
}

#pragma mark -
#pragma mark 我的下载alert
- (void)createAlert
{
    self.alert = [[UIAlertView alloc] initWithTitle:nil message:@"收藏文章后,才能下载文章" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    _alert.tintColor = [UIColor yellowColor];
    [_alert show];
    //两秒后alert消失
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(perform) userInfo:nil repeats:NO];
    [_alert release];
}
- (void)perform
{
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)createView1
{
    if ([_tableArray count] == 0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(_tableV.frame.size.width / 2, _tableV.frame.size.height / 2, 300, 60);
        [button setCenter:CGPointMake(150, 30)];
        [self.view addSubview:button];
        [button release];
    }
}

//#pragma mark -
//#pragma mark bar上的按钮
//- (void)createAddButton
//{
//
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width, 64)];
//    view.layer.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
//    view.layer.shadowOffset = CGSizeMake(0, 1);
//    view.layer.shadowOpacity = 0.2;
//    view.backgroundColor = kBlueColor;
//    [self.view addSubview:view];
//
//
//    NSArray *array = [NSArray arrayWithObjects:@"iconfont-liebiao.png", @"iconfont-45bianjiguanlixiang.png", @"iconfont-sousuo-2.png", nil];
//    for (int i = 0; i < [array count]; i++) {
//
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//
//        button.tag = i + 1;
//        [button addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
//
//        [button setImage:[UIImage imageNamed:[array objectAtIndex:i]] forState:UIControlStateNormal];
//
//        button.frame = CGRectMake((self.view.frame.size.width / 10.0f - 17) + self.view.frame.size.width / 5.0f * i * 2, 20, 34, 34);
//
//        [button setImage:[UIImage imageNamed:[array objectAtIndex:i]] forState:UIControlStateNormal];
//
//        [view addSubview:button];
//    }
//    [self.view addSubview:view];
//    [view release];
//}
//#pragma mark -
//#pragma mark button响应事件(进入列表)
//
//- (void)buttonAction1:(id)sender
//{
//    UIButton *button = (id)sender;
//    if (button.tag == 1) {//进入左侧List
//       
//
//    }else if (button.tag == 2) {//进入编辑文章
//        [self setEditing:YES animated:YES];
//
//    }else{//搜索收藏及书友
//        
//
//    }
//}


#pragma mark -
#pragma mark 个人列表
- (void)createView
{

    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)] autorelease];
    view.backgroundColor = [UIColor cyanColor];

    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    [imageV setCenter:CGPointMake(40.0f, 40.0f)];
    imageV.layer.cornerRadius = 30;
    imageV.layer.masksToBounds = YES;
    [view addSubview:imageV];
    [imageV release];

    //馆主名
    float num = (100 / 375.0f) * self.view.frame.size.width;
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(num, 20, 200, 20)];
    nameLabel.text = @"好哈腰";
    nameLabel.textColor = [UIColor purpleColor];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:nameLabel];
    [nameLabel release];

    //收藏数目及申请时间
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(num, 50, 200, 15)];
    dateLabel.text = [NSString stringWithFormat:@"%ld篇收藏", [_tableArray count]];
    [dateLabel setFont:[UIFont systemFontOfSize:13]];
    dateLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:dateLabel];
    [dateLabel release];
    _tableV.tableHeaderView = view;
}

#pragma mark -
#pragma mark 编辑收藏文章

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *cellI = @"libraryCell";
    LibraryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];
    if (!cell) {
        cell = [[[LibraryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellI] autorelease];
    }
    /******
     待编辑
     ******/
    HistoryModel *his = [_tableArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = his.history_tit;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}
#pragma mark -
#pragma mark cell样式编辑(处于编辑状态, 获得编辑样式, 提交样式)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath == 0) {
        return UITableViewCellEditingStyleInsert;
    }else
        return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_tableArray removeObjectAtIndex:indexPath.row];
        [_tableV reloadData];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
       
        [_tableV reloadData];
    }
}

#pragma mark -
#pragma mark 触发事件,返回图书界面(添加透明button,触发跳转页面事件)
- (void)createLabelCenter
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height / 2 , self.view.frame.size.width - 60, 60)];
    [label setText:@"您的收藏什么都没有,快去阅览室收藏自己喜欢的文章吧>>"];
    [_tableV setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont systemFontOfSize:18]];
    label.numberOfLines = 0;
    [label setTextColor:[UIColor blueColor]];
    [self.view addSubview:label];
    [label release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    button.backgroundColor = [UIColor clearColor];
    //[button addTarget:self action:@selector(enterBook) forControlEvents:UIControlEventTouchUpInside];
    [_tableV addSubview:button];
}

#pragma mark -
#pragma mark 保存日期(当日时间)
- (NSString *)saveDateByCollectTime
{
    NSDate *date = [NSDate date];
    NSDateFormatter *matter = [[NSDateFormatter alloc] init];
    [matter setDateFormat:@"yyyy-MM-dd hh.mm.ss"];
    NSString *dateStr = [matter stringFromDate:date];
    return dateStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)buttonCancel:(id)sender
{
    //    [UIView animateWithDuration:0.1f animations:^{
    //        _field.frame = CGRectMake(64, 4, 300, 36);
    //        [UIView animateWithDuration:0 animations:^{
    //            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //            button.frame = CGRectMake(4, 7, 30, 30);
    //            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //            [button setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    //            [_view1 addSubview:button];
    //        }];
    //    }];
    //    [[_view1.subviews objectAtIndex:1] removeFromSuperview];
    //    [_field resignFirstResponder];
}
@end
